import 'package:PollOnLine/main.dart';
import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:webview_flutter/webview_flutter.dart';

import '../../values/MyColors.dart';

/**
 * @Description: WebView 通用界面
 * @Author: 下雨了
 * @CreateDate: 2021-12-30 17:24
 * @UpdateUser: 更新者
 * @UpdateDate:  2021-12-30 17:24
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */

///
class WebViewPage extends StatefulWidget {
  final String title; // 标题
  final String url; // 访问的地址
  const WebViewPage({Key? key, required this.title, required this.url}) : super(key: key);

  @override
  _WebViewPageState createState() => _WebViewPageState();
}

class _WebViewPageState extends State<WebViewPage> {
  final WebViewController _controller = WebViewController();

  late bool progressState = true;

  @override
  void initState() {
    super.initState();
    _controller.setJavaScriptMode(JavaScriptMode.unrestricted);
    _controller.loadRequest(Uri.parse(widget.url));
    _controller.setNavigationDelegate(
      NavigationDelegate(
        onPageFinished: (String url) {
          setState(() {
            progressState = false;
          });
        },
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: MyColors.FFEDF1F5,
      appBar: BrnAppBar(title: widget.title, themeData: XxxConfigUtils.defaultAppBarConfig),
      body: Column(
        children: [
          Visibility(
            visible: progressState,
            child: LinearProgressIndicator(minHeight: 3.r, color: MyColors.FF2AAB33),
          ),
          Expanded(
            child: WebViewWidget(controller: _controller),
          ),
        ],
      ),
    );
  }
}
